<?php
require_once(dirname(__FILE__)."/config.php");
require_once(dirname(__FILE__)."/../image.func.php");
/**
* 修改dedcms图片上传到“又拍”云存储
* @林少能
* linshaoneng@sina.cn
* QQ:10832126
* http://qingphp.sinaapp.com
*/
//引入又拍云 sdk
require_once(dirname(__FILE__)."/../upyun.class.php");
//图片上传操作员账号
define( 'LKB_IMG1_USER', '' );
//图片上传密码
define( 'LKB_IMG1_PASSWD', '' );
//图片上传空间名字(bucketname)：
define( 'LKB_IMG1_BUCKETNAME', 'lkb-img' );
//图片上传图片url前缀
define( 'LKB_IMG1_URL', 'img1.lekangba.com' );

if(empty($imgfile))
{
	$imgfile='';
}
if(!is_uploaded_file($imgfile))
{
	ShowMsg("你没有选择上传的文件!".$imgfile,"-1");
	exit();
}
$imgfile_name = trim(ereg_replace("[ \r\n\t\*\%\\/\?><\|\":]{1,}",'',$imgfile_name));
if(!eregi("\.(".$cfg_imgtype.")",$imgfile_name))
{
	ShowMsg("你所上传的图片类型不在许可列表，请更改系统对扩展名限定的配置！","-1");
	exit();
}
$nowtme = time();
$sparr = Array("image/pjpeg","image/jpeg","image/gif","image/png","image/xpng","image/wbmp");
$imgfile_type = strtolower(trim($imgfile_type));
if(!in_array($imgfile_type,$sparr))
{
	ShowMsg("上传的图片格式错误，请使用JPEG、GIF、PNG、WBMP格式的其中一种！","-1");
	exit();
}
$mdir = MyDate("ymd",$nowtme);
if(!is_dir($cfg_basedir.$activepath."/$mdir"))
{
	MkdirAll($cfg_basedir.$activepath."/$mdir",$cfg_dir_purview);
	CloseFtp();
}
$filename_name = $cuserLogin->getUserID().'_'.dd2char(MyDate("His",$nowtme).mt_rand(100,999));
$filename = $mdir.'/'.$filename_name;
$fs = explode('.',$imgfile_name);
$filename = $filename.'.'.$fs[count($fs)-1];
$filename_name = $filename_name.'.'.$fs[count($fs)-1];
$fullfilename = $cfg_basedir.$activepath."/".$filename;


//------------开始上传图片到又拍，根据文件名自动生成目录-------------
$upyun = new UpYun ( LKB_IMG1_BUCKETNAME, LKB_IMG1_USER, LKB_IMG1_PASSWD );
$imgurl = LKB_IMG1_URL;
$upyun->setApiDomain ( 'v0.api.upyun.com' );
$bytes = $upyun->getBucketUsage ();

$kb = round( ($bytes / 1024 ), 4 ).'KB';
//把图片名字哈希后计算哈希字符串，提取哈希字符串前两位作为子目录名称
//强制转换图片格式成jpg格式
$md5 = md5_file($_FILES ['imgfile'] ['tmp_name']);
$path = substr($md5, 0, 3);
$file = substr($md5, 3, 29);
//$file_name = $file.'.'.end(explode('.', $_FILES ['img'] ['name']));

$filetype = imagefileType( $_FILES ['imgfile'] ['tmp_name'] );
$file_name = $file.'.'.$filetype;

$file_tmp_name = $_FILES ['imgfile'] ['tmp_name'];
$fh = fopen ( $file_tmp_name, 'rb' );
try {
    $rsp = $upyun->writeFile ( '/' . $path . '/' . $file_name, $fh, true );
}catch (Exception $e){
    //var_dump($e);
}
fclose ( $fh );
//最终的图片地址
$up_img_url = $imgurl . '/' . $path . '/' . $file_name;
//------------结束又拍-------------


//move_uploaded_file($imgfile,$fullfilename) or die("上传文件到 $fullfilename 失败！");
@unlink($imgfile);


//if(empty($resize))
//{
//	$resize = 0;
//}
//if($resize==1)
//{
//	if(in_array($imgfile_type,$cfg_photo_typenames))
//	{
//		ImageResize($fullfilename,$iwidth,$iheight);
//	}
//}
//else
//{
//	if(in_array($imgfile_type,$cfg_photo_typenames))
//	{
//		WaterImg($fullfilename,'up');
//	}
//}

//$info = '';
//$sizes[0] = 0; $sizes[1] = 0;
//$sizes = getimagesize($fullfilename,$info);
//$imgwidthValue = $sizes[0];
//$imgheightValue = $sizes[1];
//$imgsize = filesize($fullfilename);

$sizes = $kb;
$imgwidthValue = $rsp['x-upyun-width'];
$imgheightValue = $rsp['x-upyun-height'];
$imgsize = $kb;

$inquery = "INSERT INTO `#@__uploads`(arcid,title,url,mediatype,width,height,playtime,filesize,uptime,mid)
  VALUES ('0','$filename','".$up_img_url."','1','$imgwidthValue','$imgheightValue','0','{$imgsize}','{$nowtme}','".$cuserLogin->getUserID()."'); ";
$dsql->ExecuteNoneQuery($inquery);

ShowMsg("成功上传一幅图片！","select_images.php?img=".$up_img_url."&imgstick=$imgstick&comeback=".urlencode($filename_name)."&v=$v&f=$f&activepath=".urlencode($activepath)."/$mdir&d=".time());
exit();

function imagefileType($filename){
    $file = fopen($filename, "rb");
    $bin = fread($file, 2); //只读2字节
    fclose($file);
    $strInfo = @unpack("C2chars", $bin);
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
    $fileType = '';
    switch ($typeCode)
    {
        case 7790:
            $fileType = 'exe';
            break;
        case 7784:
            $fileType = 'midi';
            break;
        case 8297:
            $fileType = 'rar';
            break;
        case 8075:
            $fileType = 'zip';
            break;
        case 255216:
            $fileType = 'jpg';
            break;
        case 7173:
            $fileType = 'gif';
            break;
        case 6677:
            $fileType = 'bmp';
            break;
        case 13780:
            $fileType = 'png';
            break;
        default:
            $fileType = 'unknown: '.$typeCode;
    }

    //Fix
    if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg';
    if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png';

    return $fileType;
}

?>